package fm.dongman.modellib.source;

import android.support.annotation.NonNull;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.util.List;

import fm.dongman.animefm.contractlib.BaseDataSource;
import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.contractlib.contract.UserListContract;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;
import fm.dongman.modellib.remote.RemoteOperationHelper;

/**
 * Created by KUIGE on 2017/10/28.
 */

public class UserListRepository implements UserListContract.IUserListDataSource {
    private static final int mSize = 10; // 每页的数据量
    private static UserListRepository SINGLETON;
    private RemoteOperationHelper mRemote;
    private boolean isRemote=true;

    private UserListRepository() {
        mRemote = RemoteOperationHelper.getOperationHelper();
    }

    public static UserListRepository getInstance() {
        if (SINGLETON == null) {
            SINGLETON = new UserListRepository();
        }
        return SINGLETON;
    }

    @Override
    public void searchUsers(int page, @NotNull String key, @NotNull LoadSourceCallback<? super List<? extends IModel.IUserModel>> callback) {
        if (isRemote) {
            JSONObject jsonObject = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.SEARCH_Q + "q=" + key + "&page=" + page + "&size=" + mSize + "&type=user");
            if (jsonObject == null) {
                callback.onDataLoadFiled();
                return;
            }
            callback.onDataLoaded(mRemote.downSearchUser(jsonObject));
        }else callback.onDataLoadFiled();
    }

    @Override
    public void followUsers(int page, @NotNull String id, @NotNull LoadSourceCallback<? super List<? extends IModel.IUserModel>> callback) {
        if (isRemote) {
            JSONObject object = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.USER_LIST + "id=" + id);
            if (object == null) {
                callback.onDataLoadFiled();
                return;
            }
            callback.onDataLoaded(mRemote.downFollow(object));
        }else callback.onDataLoadFiled();
    }

    @Override
    public void fanUsers(int page, @NotNull String id, @NotNull LoadSourceCallback<? super List<? extends IModel.IUserModel>> callback) {
        if (isRemote) {
            JSONObject object = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.USER_LIST + "id=" + id);
            if (object == null) {
                callback.onDataLoadFiled();
                return;
            }
            callback.onDataLoaded(mRemote.downFans(object));
        }else callback.onDataLoadFiled();
    }

    @Override
    public void getData(@NonNull BaseDataSource.LoadSourceCallback<? super Void> callback) {
    }

    @Override
    public void saveData(Void dataModel) {
    }

    @Override
    public void refreshData() {
    }

    @Override
    public void offLine() {
        isRemote = !isRemote;
    }
}
